package com.mars.basic.controller;

import com.ag.exception.EntityNotFoundException;
import com.mars.basic.allmain.po.AdminUser;
import com.mars.basic.allmain.po.Article;
import com.mars.basic.allmain.po.QArticle;
import com.mars.basic.allmain.vo.article.ArticleSearchVo;
import com.mars.basic.allmain.vo.article.ArticleVo;
import com.mars.basic.service.ArticleService;
import com.mars.common.expetion.CodeException;
import com.mars.common.utils.AnyWhereConditionPath;
import com.mars.common.utils.QAssert;
import com.mars.common.utils.R;
import com.mars.common.utils.TenXCOS;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
@RequestMapping("/basic/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public R addArticle(@RequestBody ArticleVo vo){
        AdminUser adminUser = getSysAdminUser();
        articleService.addArticle(vo,adminUser);
        return R.ok();
    }

    /**
     * 获取更新文章
     * @param vo
     * @return
     */
    @RequestMapping("/update")
    public R updateArticle(@RequestBody ArticleVo vo){
        QAssert.notNull(vo.getId(),200,"未接受到该文章信息");
        return R.ok();
    }

    /**
     * 获取文章分页信息
     * @param vo
     * @return
     */
    @RequestMapping("/page")
    public R getArticlePage(@RequestBody ArticleSearchVo vo){
        BooleanExpression where = AnyWhereConditionPath.anyWherePredicate();
        if (vo.getSearch()!=null){
            where = where.and(QArticle.article.title.like("%"+vo.getSearch()+"%"));
        }
        OrderSpecifier<Date> desc = QArticle.article.createTime.desc();
        Page<Article> articlePage = articleService.getArticleRepository().findAll(where, vo.getPageRequest(),desc);
        return R.data(articlePage);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @RequestMapping("/deleted/{id}")
    public R deletedArticle(@PathVariable(name = "id")Integer id){
        QAssert.notNull(id, CodeException.class,"未选中信息");
        Article article = articleService.getArticleRepository().findById(id).orElse(null);
        QAssert.notNull(article, EntityNotFoundException.class);
        String userName = getSysAdminUser().getName();
        QAssert.isTrue(userName.equals(article.getAuthor()),CodeException.class,"只有本人才能删除该文本");
        articleService.getArticleRepository().deleteById(id);
        return R.ok();
    }

    @RequestMapping("/get")
    public R getArticle(@PathVariable Integer id){
        QAssert.notNull(id, CodeException.class,"未选中信息");
        Article article = articleService.getArticleRepository().findById(id).orElse(null);
        QAssert.notNull(article, EntityNotFoundException.class);
      return R.ok();
    }
}
